﻿using System;
using System.Collections.Generic;
using LoveMusic.Models;
using GSearch;
namespace Dal
{
    public class GoogleAPI
    {
        /// <summary>
        /// Get images in a database object type IMAGE for later inserting in datacontext
        /// </summary>
        /// <param name="artistName"></param>
        /// <param name="artistID"></param>
        /// <returns></returns>
        public List<IMAGE> GetImages(string artistName, int artistID)
        {
            try
            {
                List<IMAGE> listaSliki = new List<IMAGE>();
                GImageSearch gis = new GImageSearch();
                ImageSearchArgs isa = new ImageSearchArgs();
                isa.Terms = artistName + " music";
                isa.ImageType = SearchImageType.Photo;

                GImageResponse resp = gis.Search(isa);

                if (resp.Response.Results.Length > 0)
                {
                    for (int i = 0; i < resp.Response.Results.Length; i++)
                    {
                        GImageResults result = resp.Response.Results[i];

                        IMAGE googleImage = new IMAGE();
                        //googleImage.IMAGE_ID = Guid.NewGuid();
                        googleImage.ARTIST_IDZ = artistID;
                        googleImage.HEIGHT = result.Height;
                        googleImage.WIDTH = result.Width;
                        googleImage.URL = result.Url;
                        googleImage.URLTHUMB = result.TbUrl;

                        if (i == 0)
                            googleImage.TYPE = "primary";
                        else
                            googleImage.TYPE = "secondary";
                        listaSliki.Add(googleImage);

                        if (i > 10)
                            break;
                    }
                }
                return listaSliki;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public GNewsResults[] GetNews(string artistName)
        {
            try
            {
                List<Object> newsList = new List<Object>();

                GNewsSearch gns = new GNewsSearch();
                NewsSearchArgs nsa = new NewsSearchArgs();
                nsa.Terms = artistName + " music";
                GNewsResponse resp = gns.Search(nsa);
                if (resp.Response.Results.Length > 0)
                    return resp.Response.Results;
                else
                    return null;
            }
            catch
            {
                return null;
            }
        }

        public GBlogResults[] GetBlogs(string artistName)
        {
            try
            {
            List<Object> newsList = new List<Object>();

            GBlogSearch gbls = new GBlogSearch();
            BlogSearchArgs blsa = new BlogSearchArgs();
            blsa.Terms = artistName + " music";
            GBlogResponse resp = gbls.Search(blsa);
            if (resp.Response.Results.Length > 0)
                return resp.Response.Results;
            else
                return null;
            }
            catch
            {
                return null;
            }
        }       
    }
}